﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace IntelliTouchMix
{
    public partial class prueba : Form
    {
        int w, h;
        public bool expand1 = false, expand2 = false, expand3 = false, expand4 = false;
        public bool minimize1 = false, minimize2 = false, minimize3 = false, minimize4 = false;
        int avd = 50;
        int pollPeriod = 50;
        
        // Indicators indicators;
        // PlayList Util.playList;
        public prueba()
        {
            InitializeComponent();
            w = 50; h = 50;
            // tableLayoutPanel1.set
         //   this.tableLayoutPanel1.ColumnCount = 0;
           // this.tableLayoutPanel1.ColumnCount = 2;
          //      .Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 66.80851F));
          //  this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.19149F));
        }
        private void Startup()
        {
            new Thread(WorkerProc) { IsBackground = true }
            .Start();
        }

        public void maximize1()
        {
            if (expand1 || Util.zoomedPanel == Util.DECK_A)
            {
                expand1 = false;
                minimize1 = true;
            }
            else
            {
                expand1 = true;
                Util.formaPrincipal.ribbonControl1.Text = "Deck A";
            }
        }
        public void maximize2()
        {
            if (expand2 || Util.zoomedPanel == Util.DECK_B)
            {
                expand2 = false;
                minimize2 = true;
            }
            else
            {
                expand2 = true;
                Util.formaPrincipal.ribbonControl1.Text = "Deck B";

            }
        }
        public void maximize4()
        {
            if (expand3 || Util.zoomedPanel == Util.LIBRARY)
            {
                expand3 = false;
                minimize3 = true;
            }
            else
                expand3 = true;
        }
        public void maximize3()
        {
            if (expand4 || Util.zoomedPanel == Util.INDICATORS)
            {
                expand4 = false;
                minimize4 = true;
            }
            else
                expand4 = true;
        }
        public void minimizeAll()
        {

            if (expand1)
            {
                minimize1 = true;
            }else if (expand2)
            {
                minimize2 = true;
            }
            else if (expand3)
            {
                minimize4 = true;
            }
            else if (expand4)
            {
                minimize4 = true;
            }
        }
        private void WorkerProc()
        {

            while (true)
            {
                var et = Environment.TickCount;

                // call your methods
                try
                {
                    if (InvokeRequired)
                    {
                        // after we've done all the processing, 
                        this.Invoke(new MethodInvoker(delegate
                        {
                            // load the control with the appropriate data
                            updatePanel1();
                        }));
                        // return;
                    }
                }
                catch (Exception e)
                {

                }
                et = Environment.TickCount - et;
                var sleep = pollPeriod - et;
                if (sleep < 0) sleep = 0; // always yield
                Thread.Sleep(sleep);
            }
        }
        void updatePanel1()
        {/*
            Util.deckA.Invoke((MethodInvoker)delegate
            {if (Util.zoomedPanel == Util.DECK_A)
                {
                    // Util.centerForm.expand1 = false;
                    Util.centerForm.minimize1 = false;
                }else if (Util.zoomedPanel == Util.DECK_B)
                {
                    // Util.centerForm.expand1 = false;
                    Util.centerForm.minimize2 = false;
                }
                else if (Util.zoomedPanel == Util.MONITOR_PLAYER)
                {
                    // Util.centerForm.expand1 = false;
                    Util.centerForm.minimize3 = false;
                }
                else if (Util.zoomedPanel == Util.INDICATORS)
                {
                    // Util.centerForm.expand1 = false;
                    Util.centerForm.minimize4 = false;
                }
                Util.centerForm.expand1 = Util.DECK_A == Util.zoomedPanel;
              //  Util.centerForm.minimize1 = !(Util.DECK_A == Util.zoomedPanel);
                Util.centerForm.expand2 = Util.DECK_B == Util.zoomedPanel;
             //   Util.centerForm.minimize2 = !(Util.DECK_B == Util.zoomedPanel);
                Util.centerForm.expand3 = Util.MONITOR_PLAYER == Util.zoomedPanel;
            //    Util.centerForm.minimize3 = !(Util.INDICATORS == Util.zoomedPanel);
                Util.centerForm.expand4 = Util.INDICATORS == Util.zoomedPanel; 
          //      Util.centerForm.minimize4 = !(Util.MONITOR_PLAYER == Util.zoomedPanel);
            });*//*
            if (Util.zoomedPanel == Util.DECK_A)
                {
                    // Util.centerForm.expand1 = false;
                    Util.centerForm.minimize1 = false;
                }else if (Util.zoomedPanel == Util.DECK_B)
                {
                    // Util.centerForm.expand1 = false;
                    Util.centerForm.minimize2 = false;
                }
                else if (Util.zoomedPanel == Util.MONITOR_PLAYER)
                {
                    // Util.centerForm.expand1 = false;
                    Util.centerForm.minimize3 = false;
                }
                else if (Util.zoomedPanel == Util.INDICATORS)
                {
                    // Util.centerForm.expand1 = false;
                    Util.centerForm.minimize4 = false;
                }*/
            if (expand1)
            {
                //Controls.Remove(tableLayoutPanel1);
                //Controls.Add(groupPanel1);
                expand1 = false;

                w = 100;
                h = 100;
                tableLayoutPanel1.ColumnStyles[0].Width = w;
                tableLayoutPanel1.RowStyles[0].Height = h;
                tableLayoutPanel1.ColumnStyles[1].Width = 100 - w;
                tableLayoutPanel1.RowStyles[1].Height = 100 - h;
                /*
                if (tableLayoutPanel1.ColumnStyles[0].Width >= 100 && tableLayoutPanel1.RowStyles[0].Height >= 100)
                {
                    expand1 = false; return;
                }
                w += avd;
                h += avd;
                tableLayoutPanel1.ColumnStyles[0].Width = w;
                tableLayoutPanel1.RowStyles[0].Height = h;
                tableLayoutPanel1.ColumnStyles[1].Width = 100 - w;
                tableLayoutPanel1.RowStyles[1].Height = 100 - h;
            /*    tableLayoutPanel1.Update();
                groupPanel1.Update();
                groupPanel2.Update();
                groupPanel3.Update();
                groupPanel4.Update();*/
                // label1.Text = "t " + tableLayoutPanel1.ColumnStyles[0].Width + "\nr" + tableLayoutPanel1.RowStyles[1].Height;
            } 
            else if (minimize1)
            {
                //Controls.Remove(groupPanel1);
                minimize1 = false;

                w = 50;
                h = 50;
                tableLayoutPanel1.ColumnStyles[0].Width = w;
                tableLayoutPanel1.RowStyles[0].Height = h;
                tableLayoutPanel1.ColumnStyles[1].Width = 100 - w;
                tableLayoutPanel1.RowStyles[1].Height = 100 - h;
                //Controls.Add(tableLayoutPanel1);
                //tableLayoutPanel1.Dock = DockStyle.Fill;
                /*
                if (tableLayoutPanel1.ColumnStyles[0].Width <= 50 && tableLayoutPanel1.RowStyles[0].Height <= 50)
                {
                    minimize1 = false;
                    return;
                }
                w -= avd;
                h -= avd;
                tableLayoutPanel1.ColumnStyles[0].Width = w;
                tableLayoutPanel1.RowStyles[0].Height = h;
                tableLayoutPanel1.ColumnStyles[1].Width = 100 - w;
                tableLayoutPanel1.RowStyles[1].Height = 100 - h;
              /*  tableLayoutPanel1.Update();
                groupPanel1.Update();
                groupPanel2.Update();
                groupPanel3.Update();
                groupPanel4.Update();*/
                // label1.Text = "t " + tableLayoutPanel1.ColumnStyles[0].Width + "\nr" + tableLayoutPanel1.RowStyles[1].Height;
            }
            else if (minimize2)
            {
                if (tableLayoutPanel1.ColumnStyles[0].Width <= 50 && tableLayoutPanel1.RowStyles[0].Height <= 50)
                {
                    minimize2 = false;
                    return;
                }
                w -= avd;
                h -= avd;
                tableLayoutPanel1.ColumnStyles[0].Width = 100-w;
                tableLayoutPanel1.RowStyles[0].Height = h;
                tableLayoutPanel1.ColumnStyles[1].Width = w;
                tableLayoutPanel1.RowStyles[1].Height = 100 - h;
             /*   tableLayoutPanel1.Update();
                groupPanel1.Update();
                groupPanel2.Update();
                groupPanel3.Update();
                groupPanel4.Update();*/
                // label1.Text = "t " + tableLayoutPanel1.ColumnStyles[0].Width + "\nr" + tableLayoutPanel1.RowStyles[1].Height;
             
            }
            else if (expand2)
            {
                if (tableLayoutPanel1.ColumnStyles[1].Width >= 100 && tableLayoutPanel1.RowStyles[0].Height >= 100)
                {
                    expand2 = false;
                    return;
                }
                w += avd;
                h += avd;
                tableLayoutPanel1.ColumnStyles[0].Width = 100 - w;
                tableLayoutPanel1.RowStyles[0].Height = h;
                tableLayoutPanel1.ColumnStyles[1].Width = w;
                tableLayoutPanel1.RowStyles[1].Height = 100 - h;
                /*tableLayoutPanel1.Update();
                groupPanel1.Update();
                groupPanel2.Update();
                groupPanel3.Update();
                groupPanel4.Update();*/
                // label1.Text = "t " + tableLayoutPanel1.ColumnStyles[0].Width + "\nr" + tableLayoutPanel1.RowStyles[1].Height;
            }
            else if (expand3)
            {
                if (tableLayoutPanel1.ColumnStyles[1].Width >= 100 && tableLayoutPanel1.RowStyles[1].Height >= 100)
                {
                    expand3 = false; return;
                }
                w += avd;
                h += avd;
                tableLayoutPanel1.ColumnStyles[0].Width =100 - w;
                tableLayoutPanel1.RowStyles[0].Height = 100 - h;
                tableLayoutPanel1.ColumnStyles[1].Width =  w;
                tableLayoutPanel1.RowStyles[1].Height = h;
               /* tableLayoutPanel1.Update();
                groupPanel1.Update();
                groupPanel2.Update();
                groupPanel3.Update();
                groupPanel4.Update();*/
                // label1.Text = "t " + tableLayoutPanel1.ColumnStyles[0].Width + "\nr" + tableLayoutPanel1.RowStyles[1].Height;
            }
            else if (minimize3)
            {
                if (tableLayoutPanel1.ColumnStyles[1].Width <= 50 && tableLayoutPanel1.RowStyles[1].Height <= 50)
                {
                    minimize3 = false;
                    return;
                }
                w -= avd;
                h -= avd;
                tableLayoutPanel1.ColumnStyles[0].Width = 100 - w;
                tableLayoutPanel1.RowStyles[0].Height =100 -  h;
                tableLayoutPanel1.ColumnStyles[1].Width = w;
                tableLayoutPanel1.RowStyles[1].Height = h;
               /* tableLayoutPanel1.Update();
                groupPanel1.Update();
                groupPanel2.Update();
                groupPanel3.Update();
                groupPanel4.Update();*/
                // label1.Text = "t " + tableLayoutPanel1.ColumnStyles[0].Width + "\nr" + tableLayoutPanel1.RowStyles[1].Height;
            }
            else if (expand4)
            {
                if (tableLayoutPanel1.ColumnStyles[0].Width >= 100 && tableLayoutPanel1.RowStyles[1].Height >= 100)
                {
                    expand4 = false; return;
                }
                w += avd;
                h += avd;
                tableLayoutPanel1.ColumnStyles[0].Width = w;
                tableLayoutPanel1.RowStyles[0].Height =100 - h;
                tableLayoutPanel1.ColumnStyles[1].Width = 100 - w;
                tableLayoutPanel1.RowStyles[1].Height =  h;
             /*   tableLayoutPanel1.Update();
                groupPanel1.Update();
                groupPanel2.Update();
                groupPanel3.Update();
                groupPanel4.Update();*/
                // label1.Text = "t " + tableLayoutPanel1.ColumnStyles[0].Width + "\nr" + tableLayoutPanel1.RowStyles[1].Height;
            }
            else if (minimize4)
            {
                if (tableLayoutPanel1.ColumnStyles[0].Width <= 50 && tableLayoutPanel1.RowStyles[1].Height <= 50)
                {
                    minimize4 = false;
                    return;
                }
                w -= avd;
                h -= avd;
                tableLayoutPanel1.ColumnStyles[0].Width = w;
                tableLayoutPanel1.RowStyles[0].Height =100 -  h;
                tableLayoutPanel1.ColumnStyles[1].Width = 100 - w;
                tableLayoutPanel1.RowStyles[1].Height = h;
            /*    tableLayoutPanel1.Update();
                groupPanel1.Update();
                groupPanel2.Update();
                groupPanel3.Update();
                groupPanel4.Update();*/
                // label1.Text = "t " + tableLayoutPanel1.ColumnStyles[0].Width + "\nr" + tableLayoutPanel1.RowStyles[1].Height;
            }
        }
       
       
        private void prueba_Load(object sender, EventArgs e)
        {
            Startup();
            // fill the four panels with stuff            
            Util.dockFormInContainer(Util.deckA);
            Util.dockFormInContainer(Util.deckB);
            Util.dockFormInContainer(Util.playList);
            Util.dockFormInContainer(Util.indicatorForm);
            Util.deckA.volume.Select();
            tableLayoutPanel1.Controls.Add(Util.deckA, 0, 0);
            tableLayoutPanel1.Controls.Add(Util.deckB, 1, 0);
            tableLayoutPanel1.Controls.Add(Util.indicatorForm, 0, 1);
            tableLayoutPanel1.Controls.Add(Util.playList, 1, 1);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            expand1=true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            minimize1 = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            expand2 = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            minimize2 = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            expand3 = true;

        }

        private void button6_Click(object sender, EventArgs e)
        {
            minimize3 = true;

        }

        private void button7_Click(object sender, EventArgs e)
        {
            expand4 = true;

        }

        private void button8_Click(object sender, EventArgs e)
        {
            minimize4 = true;

        }
    }
}
